﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using SweeperPrototype.GUI.Behaviors.AttachedCommand;
using SweeperPrototype.GUI.EffectiveExposures;
using SweeperPrototype.GUI.Positions;
using SweeperPrototype.Objects;

namespace SweeperPrototype.GUI.EffectiveExposures
{
    public class EffectiveExposureListViewModel : BaseVM
    {
        private readonly ObservableCollection<EffectiveExposureViewModel> m_EffectiveExposures;
        private readonly INotifyList<EffectiveExposure> m_CoreMappings;
        private readonly NotifyList<EffectiveExposure> m_SelectedMappings;
        private readonly SimpleCommand m_GridSelectionHandler;

        private readonly NotifyList<EffectiveExposureViewModel> m_SelectedViewModels;
        private readonly NotifyList<EffectiveExposure> m_SelectedPositions;

        public EffectiveExposureListViewModel(NotifyList<EffectiveExposure> core_mappings)
        {
            m_CoreMappings = core_mappings;
            m_SelectedMappings = new NotifyList<EffectiveExposure>();
            m_EffectiveExposures = new ObservableCollection<EffectiveExposureViewModel>();

            m_CoreMappings.Addition += HandleCoreMappingAdded;
            m_CoreMappings.Removal += HandleCoreMappingRemoved;

            // TODO: clean this up. Its fugly.
            //m_GridSelectionHandler = new SimpleCommand(o => true,
            //    delegate(object o)
            //    {
            //        ObservableCollection<Object> collection = o as ObservableCollection<Object>;

            //        List<EffectiveExposureViewModel> removal_list = new List<EffectiveExposureViewModel>(m_SelectedViewModels);

            //        foreach (EffectiveExposureViewModel p in collection)
            //        {
            //            if (removal_list.Remove(p) == false)
            //            {
            //                m_SelectedViewModels.Add(p);
            //                m_SelectedPositions.Add(p.CoreEffectiveExposure);
            //            }
            //        }

            //        foreach (EffectiveExposureViewModel position_view_model in removal_list)
            //        {
            //            // todo: use indexes instead.
            //            m_SelectedViewModels.Remove(position_view_model);
            //            m_SelectedPositions.Remove(position_view_model.CoreEffectiveExposure);
            //        }
            //    }
            //    );
        }


        
        public SimpleCommand GridSelectionHandler
        {
            get { return m_GridSelectionHandler; }
        }
        public ObservableCollection<EffectiveExposureViewModel> EffectiveExposures
        {
            get { return m_EffectiveExposures; }
        }

        public INotifyList<EffectiveExposure> SelectedMappings { get; set; }

        private void HandleCoreMappingRemoved(EffectiveExposure removal, int index)
        {
            m_EffectiveExposures.RemoveAt( index );
        }

        private void HandleCoreMappingAdded(EffectiveExposure addition, int index)
        {
            EffectiveExposureViewModel mapping_view_model = new EffectiveExposureViewModel(addition);
            m_EffectiveExposures.Add( mapping_view_model );
            mapping_view_model.PropertyChanged += HandleMappingSelected;
        }

        private void HandleMappingSelected(object sender, PropertyChangedEventArgs args)
        {
            
            if( args == EffectiveExposureViewModel.ARGS_ISSELECTED )
            {
                EffectiveExposureViewModel view_model = (EffectiveExposureViewModel)sender;
                if( view_model.IsSelected )
                {
                    m_SelectedMappings.Add(view_model.CoreEffectiveExposure);
                }
                else
                {
                    m_SelectedMappings.Remove(view_model.CoreEffectiveExposure);
                }
            }
        }

    }
}
